import React from "react"
import { Redirect, Route, Switch } from "react-router-dom"

const beforeEnter = (item, History) => {

    if (item.path === '/home') {
        let token = window.localStorage.getItem("token")
        if (!token) {
            return <Redirect to='/login' />
        }
    }
    return <item.component route={item.children ? item.children : []} {...History}>
        <RouteView route={item.children ? item.children : []}></RouteView>
    </item.component>
}

const RouteView = (props) => {
    let { route } = props
    return <Switch>
        {
            route.map((item, index) => item.path ? <Route
                path={item.path}
                key={index}
                render={(History) => beforeEnter(item, History)}
            /> : <Redirect {...item} key={index} exact />)
        }
    </Switch>
}

export default RouteView